﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeostatisticalTool.Classes;

namespace GeostatisticalTool.Forms
{
    public partial class PredictedValue : UserControl
    {

        public event EventHandler ChangedCoordinate;
        protected virtual void OnChangedCoordinater(EventArgs e)
        {
            if (ChangedCoordinate != null)
                ChangedCoordinate(this, e);
        }

        public PredictedValue()
        {
            InitializeComponent();
        }
        private double x;

        public double X
        {
            get { return x; }
            set { x = value;
                  uxX.Text= x.ToString();
            }
        }
        private double y;

        public double Y
        {
            get { return y; }
            set { y = value;
            uxY.Text = y.ToString();
            }
        }
        private double w;

        public double W
        {
            get { return w; }
            set { w = value;
             uxValue.Text = w.ToString();
            }
        }

        private double z;

        public double Z
        {
            get { return z; }
            set
            {
                z = value;
                uxZ.Text = z.ToString();
            }
        }




        private IInterpolator inter;

        public void SetCoor(IInterpolator inter,double x, double y, double z)
        {
            this.inter = inter;
            this.X = x;
            this.Y = y;
            this.Z = z;
            this.W = inter.Interpolate(x, y,z,true)[0];
            uxValue.Text = this.W.ToString();
            OnChangedCoordinater(EventArgs.Empty);
        }

        public void SetInterpolator(IInterpolator inter)
        {
            this.inter=inter;
        }

        private void uxX_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxX))
            {
                x = Convert.ToDouble(uxX.Text);
                uxApply.Enabled = true;
            }
            
            //if (inter != null)
            //{
            //    this.z = inter.Interpolate(x, y, true);
            //    uxValue.Text = this.z.ToString();
            //}
            //OnChangedCoordinater(EventArgs.Empty);

        }

        private void uxY_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxY))
            {
                y = Convert.ToDouble(uxY.Text);
                uxApply.Enabled = true;
            }
            //if (inter != null)
            //{
            //    this.z = inter.Interpolate(x, y, true);
            //    uxValue.Text = this.z.ToString();
            //}
            //OnChangedCoordinater(EventArgs.Empty);
        }

        public void Recalculate()
        {
            if (inter != null)
            {
                this.w = inter.Interpolate(x, y,z, true)[0];
                uxValue.Text = this.w.ToString();
                uxApply.Enabled = false;
            }
        }

        private void uxApply_Click(object sender, EventArgs e)
        {
            Recalculate();
            OnChangedCoordinater(EventArgs.Empty);
        }

        public void EnableApply(bool value)
        {
            uxApply.Enabled = value;
        }

        private void uxZ_TextChanged(object sender, EventArgs e)
        {
            if (Validator.IsDouble(uxZ))
            {
                z = Convert.ToDouble(uxZ.Text);
                uxApply.Enabled = true;
            }
        }
    }
}
